package com.huawei.codearts.controller;

import com.huawei.codearts.model.Member;
import com.huawei.codearts.service.EsApiService;
import com.huawei.codearts.service.EsService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/es")
public class ElasticsearchController {

    private final EsService esService;

    @Autowired
    public ElasticsearchController(EsService esService) {
        this.esService = esService;
    }

    @RequestMapping("/save")
    public String save(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String user = (String)params.get("user");
        String name = (String)params.get("name");
        String status = (String)params.get("status");
        String money = (String)params.get("money");
        String country = (String)params.get("country");
        String description = (String)params.get("description");

        Member member = new Member();
        member.setId(Integer.parseInt(id));
        member.setUser(user);
        member.setName(name);
        member.setCountry(country);
        member.setDescription(description);
        member.setStatus(Integer.parseInt(status));
        member.setMoney(BigDecimal.valueOf(Double.parseDouble(money)));
        long timestamp = System.currentTimeMillis();
        member.setCreatetime(timestamp);
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        member.setCreatetimeFormat(formattedDate);

        String result;
        try {
            result = ResponseUtils.response(esService.asve(member));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/delId")
    public String delById(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String result;
        try {
            result = ResponseUtils.response(esService.del(id));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/del")
    public String delete(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        Member member = new Member();
        member.setId(Integer.parseInt(id));
        String result;
        try {
            result = ResponseUtils.response(esService.del(member));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/find")
    public String find(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esService.find());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findById")
    public String findById(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String result;
        try {
            result = ResponseUtils.response(esService.findById(id));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findByIdAll")
    public String findByIdAll(@RequestParam Map<String, Object> params) {
        String ids = (String)params.get("ids");
        ArrayList<String> idsList = null;
        if (!Objects.equals(ids, null)) {
            idsList = new ArrayList<>(Arrays.asList(ids.split(",")));
        }

//        ArrayList<String> ids = new ArrayList<>();
//        ids.add("1");
//        ids.add("2");
//        ids.add("3");

        String result;
        try {
            result = ResponseUtils.response(esService.findByIdAll(idsList));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findCount")
    public String findCount(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esService.findCount());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/existsById")
    public String existsById(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String result;
        try {
            result = ResponseUtils.response(esService.existsById(id));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findByLike")
    public String findByLike(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esService.findByLike());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findByDescription")
    public String findByDescription(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esService.findByDescription());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/search")
    public String search(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esService.search());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

}
